<template>
  <el-form
    :model="ruleForm"
    status-icon
    :rules="rules"
    ref="ruleForm"
    label-width="100px"
    class="demo-ruleForm"
    style="padding: 15px"
  >
    <el-form-item label="原密码">
      <el-input
        v-model="inputpwd"
        show-password
        class="w200"
        @change="confirmpwd"
      ></el-input>
    </el-form-item>
    <el-form-item label="新密码" prop="pass">
      <el-input class="w200" type="password" v-model="ruleForm.pass"></el-input>
    </el-form-item>
    <el-form-item label="确认密码" prop="checkPass">
      <el-input
        class="w200"
        type="password"
        v-model="ruleForm.checkPass"
        autocomplete="off"
      ></el-input>
    </el-form-item>
    <el-form-item style="margin-left: 23px">
      <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
      <el-button @click="resetForm('ruleForm')">重置</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
import { Editpwd, checkOriginpwd } from "@/apis/users";

export default {
  data() {
    var validatePass = (rules, value, callback) => {
      if (value === "") {
        callback(new Error("请输入密码"));
      } else {
        if (this.ruleForm.checkPass !== "") {
          this.$refs.ruleForm.validateField("checkPass");
        }
        callback();
      }
    };
    var validatePass2 = (rules, value, callback) => {
      if (value === "") {
        callback(new Error("请再次输入密码"));
      } else if (value !== this.ruleForm.pass) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    };
    return {
      originPwd: "", //用户本身密码
      inputpwd: "", //用户自身输入密码
      ruleForm: {
        pass: "",
        checkPass: "",
        userId: "",
      },
      rules: {
        pass: [{ validator: validatePass, trigger: "blur" }],
        checkPass: [{ validator: validatePass2, trigger: "blur" }],
      },
    };
  },
  methods: {
    //确认密码
    confirmpwd(v) {
      console.log(v);
      checkOriginpwd({ oldPwd: v, id: this.userId }).then((res) => {
        if (res.data.code == 0) {
          this.$message({
            type: "success",
            message: "您输入的密码正确",
          });
        }
        else{
           this.$message({
            type: "warning",
            message: "您输入的密码和原密码不一致,请重新输入",
          })
        }
      })
    },
    //修改密码
    submitForm(formName) {  
      this.$refs[formName].validate((valid) => {
        if (valid) {
          Editpwd({ newPwd: this.ruleForm.pass, id: this.userId }).then(
            (res) => {
              console.log(res);
              if (res.data.code == 0) {
                 this.$message({
            type: "success",
            message: "密码修改成功,请重新登录",
          });
              }
            }
          );
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
  created() {
    this.userId = localStorage.id;
  },
};
</script>

<style lang="less" scoped>
.w200 {
  width: 200px;
}
</style>